package com.appcheck.recording.listener.impl;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;

import com.appcheck.recording.ListenerManager;
import com.appcheck.recording.WidgetHelper;
import com.appcheck.recording.action.impl.ItemClickAction;
import com.appcheck.recording.listener.AbstractListener;
import com.appcheck.recording.utils.Logger;

public class ItemLongClickListener extends AbstractListener implements
		OnItemLongClickListener {
	private int b;
	
	public ItemLongClickListener(View paramView) {
	    super(paramView, AdapterView.class, AdapterView.OnItemLongClickListener.class);
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		 if (this.b++ > 1) {
		      return false;
		 }
	    ItemClickAction itemClickAction = new ItemClickAction();
		//itemClickAction.setLine(position - parent.getFirstVisiblePosition());
		//这里改为记录相对于ApdaterView的position,以前是减去第一次出现的位置
		itemClickAction.setLine(position);
		Logger.INFO("onItemLongClick position = " + position + ", id = " + id + ", " +
				"parent.getPositionForView = " + parent.getPositionForView(view));
	    itemClickAction.setLongClick(true);
	    itemClickAction.setParentResourceName(WidgetHelper.getResourceName(parent));
	    ListenerManager.getListenerManager().handleAction(itemClickAction);
	     if (this.listener != null){
	      boolean bool = ((AdapterView.OnItemLongClickListener)this.listener).onItemLongClick(parent, view, position, id); 
	      this.b = 0;
	      return bool;
	    }
	     this.b = 0;
	     return true;
	}
	@Override
	protected final Object getListenerFromView(View paramView, Class viewClass, Class listenerClass){
		OnItemLongClickListener onItemLongClickListener = ((AdapterView)paramView).getOnItemLongClickListener();
		if(this.listener != null){
			Logger.INFO(new StringBuilder().append(paramView.getClass().getSimpleName()).append(" listener remembered").toString());
		}
		return onItemLongClickListener;
	}
}
